package cn.wizzer.app.web.modules.controllers.platform.cms;

import cn.wizzer.app.user.modules.models.Banner;
import cn.wizzer.app.user.modules.services.BannerService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.PageUtil;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/cms/banner")
public class CmsBannerController {
    private final static Log log = Logs.get();
    @Inject
    @Reference
    private BannerService bannerService;

    @At("/")
    @Ok("beetl:/platform/cms/banner/index.html")
    @RequiresPermissions("cms.banner.list")
    public void index() {

    }

    /**
     * banner列表
     * @param pageNumber int
     * @param pageSize int
     * @param pageOrderName String
     * @param pageOrderBy String
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.banner.list")
    public Object data(@Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        try {
            Cnd cnd = Cnd.NEW();
            if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
                cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
            }
            return Result.success().addData(bannerService.listPage(pageNumber, pageSize, cnd));
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 新建Banner
     * @param banner Banner
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("cms.banner.add")
    @SLog(tag = "新建Banner")
    public Object add(@Param("..") Banner banner) {
        try {
            // 调用校验方法
            Result result = bannerService.validateBanner(banner);
            // 校验成功则插入
            if(result.getCode() == 0){
                bannerService.insert(banner);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 删除Banner
     * @param id String
     * @return Object
     */
    @At({"/delete/?","/delete"})
    @Ok("json")
    @RequiresPermissions("cms.banner.delete")
    public Object delete(String id, @Param("ids") String[] ids) {
        try {
            if (null != ids && ids.length > 0) {
                bannerService.delete(ids);
            } else {
                bannerService.delete(id);
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }

    }

    /**
     * 启用Banner
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("cms.banner.edit")
    @SLog(tag = "启用Banner")
    public Object enable(String id) {
        try {
            bannerService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 禁用Banner
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("cms.banner.edit")
    @SLog(tag = "禁用Banner")
    public Object disable(String id) {
        try {
            bannerService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }


    /**
     * 查询Banner
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(String id) {
        try {
            return Result.success("system.success", bannerService.fetch(id));
        } catch (Exception e) {
            return Result.error("system.error");
        }
    }


    /**
     * 修改Banner
     * @param banner Banner
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("cms.banner.edit")
    @SLog(tag = "修改Banner")
    public Object editDo(@Param("..") Banner banner) {
        try {
            Result result = bannerService.validateBanner(banner);
            if(result.getCode() == 0){
                bannerService.update(banner);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error();
        }
    }


}




